// 根据id查找班级信息
import express from "express";
import pool from "../importantApi/db_connection.js";

const router = express.Router();
router.get("/", async (req, res) => {
    const {classId} = req.query;
    if (!classId) {
        return res.status(401).json({
            code: 401,
            msg: "缺少必要参数"
        })
    }
    try {
        let [result] = await pool.query(`
            select c.class_id,
                   c.class_name,
                   u.user_name as teacher_name
            from classinfo c
                     join userinfo u
                          on c.user_id = u.user_id
            where c.class_id = ${classId}
        `);
        if (result.length > 0) {
            return res.status(200).json({
                code: 200,
                data: result[0]
            })
        } else {
            return res.status(201).json({
                code: 201,
                msg: "班级信息不存在"
            })
        }
    } catch (err) {
        console.log(err)
        res.status(500).json({
            code: 500,
            msg: "查找班级错误：server error"
        })
    }
})
export default router;